import java.util.LinkedList;
import java.util.Queue;

/**
 * @Date 2022/7/11
 * @Author YuanYuan
 * @Time 9:30
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //根节点
    public TreeNode root;
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        this.root = A;
        return A;
    }
    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *以前序遍历的方式
     * @return
     */
    public int i = 0;
    public TreeNode createTree1(String s) {
        if(null == s) {
            return null;
        }
        TreeNode cur = new TreeNode(s.charAt(i));
        i++;
        if('#' != cur.val) {
            if (null == cur.left) {
                cur.left = createTree1(s);
            }
            if (null == cur.right) {
                cur.right = createTree1(s);
            }
        }
        return cur;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        //根左右
        if(root == null) {
            return;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        //左根右
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root);
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        //左右根
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root);
    }



    // 子问题思路  获取树中节点的个数
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) +size(root.right) + 1;

    }

    //遍历思路：只要遍历到了节点 就nodeSize ++
    public static int nodeSize;
    void size2(TreeNode root) {
        if(root == null) {
            return;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }

    //子问题 获取叶子节点的个数
    int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //遍历思路
    public static int leafSize;
    void getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k) {

        if(root == null) {
            return 0;
        }

        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +getKLevelNodeCount(root.right,k-1);

    }

    public static int levelSize;
    void getKLevelNodeCount2(TreeNode root,int k) {
        if(root == null) {
            return;
        }
        if(k == 1) {
            levelSize++;
        }
        getKLevelNodeCount2(root.left,k-1);
        getKLevelNodeCount2(root.right,k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }

        return Math.max(getHeight(root.left),getHeight(root.right)) + 1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left,val);

        if(ret1 != null) {
            return ret1;
        }

        TreeNode ret2 = find(root.right,val);
        if(ret2 != null) {
            return ret2;
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if(null == root) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val +" ");
            if(null!=top.left) {
                queue.add(top.left);
            }
            if(null!=top.right) {
                queue.add(top.right);
            }
        }
    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if(null == root) {
            return false;
        }
        int flag = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if('#' == top.val) {
                flag++;
            }
            if(0!=flag&&'#'!=top.val) {
                return false;
            }
            if(null!=top.left) {
                queue.add(top.left);
            }
            if(null!=top.right) {
                queue.add(top.right);
            }
        }
        return true;
    }
}
